package com.huike.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.clues.domain.dto.MPTbCourseDTO;
import com.huike.clues.mapper.MPCourseMapper;
import com.huike.common.core.page.PageDomain;
import com.huike.common.core.page.TableSupport;
import com.huike.common.entity.MPTbActivity;
import com.huike.common.entity.MPTbContract;
import com.huike.common.utils.StringUtils;
import com.huike.contract.mapper.ContracMapper;
import com.huike.contract.service.ContracService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description TODO
 * @Author hxd
 * @Date 2023-08-30 22:42
 */
@Service
public class ContracServiceImpl extends ServiceImpl<ContracMapper,MPTbContract> implements ContracService {

    @Resource
    private ContracMapper contracMapper;
    @Resource
    private MPCourseMapper courseMapper;

    /**
     * 查询合同列表
     * @param mpTbContract
     * @return
     */
    @Override
    public Page<MPTbContract> contractList(MPTbContract mpTbContract) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        PageHelper.startPage(pageDomain.getPageNum(),pageDomain.getPageSize());

        QueryWrapper<MPTbContract> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(mpTbContract.getContractNo()),"contract_no",mpTbContract.getContractNo());
        wrapper.like(StringUtils.isNotEmpty(mpTbContract.getName()),"name",mpTbContract.getName());
        wrapper.like(StringUtils.isNotEmpty(mpTbContract.getPhone()),"phone",mpTbContract.getPhone());
        wrapper.eq(ObjectUtils.isNotEmpty(mpTbContract.getSubject()),"subject",mpTbContract.getSubject());
        wrapper.eq(ObjectUtils.isNotEmpty(mpTbContract.getCourseId()),"course_id",mpTbContract.getCourseId());

        if(mpTbContract.getParams()!= null){
            wrapper.ge(ObjectUtils.isNotEmpty(mpTbContract.getParams().get("beginCreateTime")),"create_time",mpTbContract.getParams().get("beginCreateTime"));
            wrapper.le(ObjectUtils.isNotEmpty(mpTbContract.getParams().get("endCreateTime")),"create_time",mpTbContract.getParams().get("endCreateTime"));
        }
        List<MPTbContract> mpSysDictTypeList = contracMapper.selectList(wrapper);
        Page<MPTbContract> pageList = (Page<MPTbContract>) mpSysDictTypeList;

        return pageList;
    }

    /**
     * 获取合同信息2
     * @param id
     * @return
     */
    @Override
    public MPTbContract findId(Long id) {
        MPTbContract mpTbContract = contracMapper.selectById(id);
        //订单价格
        MPTbCourseDTO mpTbCourseDTO = courseMapper.selectById(mpTbContract.getCourseId());
        mpTbContract.setOrder(mpTbCourseDTO.getPrice());
        return mpTbContract;
    }
}
